<template>
  <el-dialog
    :title="!middle.seckillMiddleId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="middle" :rules="dataRule" ref="middle" @keyup.enter.native="middleSubmit()" label-width="80px">
    <el-form-item label="商品id" prop="shopId">
      <el-input v-model="middle.shopId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="秒杀活动id" prop="seckillId">
      <el-input v-model="middle.seckillId" placeholder="秒杀活动id"></el-input>
    </el-form-item>
    <el-form-item label="订单id" prop="orderId">
      <el-input v-model="middle.orderId" placeholder="订单id"></el-input>
    </el-form-item>
    <el-form-item label="秒杀价格" prop="seckillPrice">
      <el-input v-model="middle.seckillPrice" placeholder="秒杀价格"></el-input>
    </el-form-item>
    <el-form-item label="秒杀状态" prop="seckillStatus">
      <el-input v-model="middle.seckillStatus" placeholder="秒杀状态"></el-input>
    </el-form-item>
    <el-form-item label="秒杀库存" prop="seckillStockNum">
      <el-input v-model="middle.seckillStockNum" placeholder="秒杀库存"></el-input>
    </el-form-item>
    <el-form-item label="是否限购" prop="isPurchase">
      <el-input v-model="middle.isPurchase" placeholder="是否限购"></el-input>
    </el-form-item>
    <el-form-item label="限购数量" prop="purchaseNum">
      <el-input v-model="middle.purchaseNum" placeholder="限购数量"></el-input>
    </el-form-item>
    <el-form-item label="是否包邮" prop="isExpressage">
      <el-input v-model="middle.isExpressage" placeholder="是否包邮"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="middleSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        middle: {
          seckillMiddleId: 0,
          shopId: '',
          seckillId: '',
          orderId: '',
          seckillPrice: '',
          seckillStatus: '',
          seckillStockNum: '',
          isPurchase: '',
          purchaseNum: '',
          isExpressage: ''
        },
        dataRule: {
          shopId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          seckillId: [
            { required: true, message: '秒杀活动id不能为空', trigger: 'blur' }
          ],
          orderId: [
            { required: true, message: '订单id不能为空', trigger: 'blur' }
          ],
          seckillPrice: [
            { required: true, message: '秒杀价格不能为空', trigger: 'blur' }
          ],
          seckillStatus: [
            { required: true, message: '秒杀状态不能为空', trigger: 'blur' }
          ],
          seckillStockNum: [
            { required: true, message: '秒杀库存不能为空', trigger: 'blur' }
          ],
          isPurchase: [
            { required: true, message: '是否限购不能为空', trigger: 'blur' }
          ],
          purchaseNum: [
            { required: true, message: '限购数量不能为空', trigger: 'blur' }
          ],
          isExpressage: [
            { required: true, message: '是否包邮不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.middle.seckillMiddleId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['middle'].resetFields()
          if (this.middle.seckillMiddleId) {
            this.$http({
              url: this.$http.adornUrl(`/seckill/middle/info/${this.middle.seckillMiddleId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.middle.shopId = data.middle.shopId
                this.middle.seckillId = data.middle.seckillId
                this.middle.orderId = data.middle.orderId
                this.middle.seckillPrice = data.middle.seckillPrice
                this.middle.seckillStatus = data.middle.seckillStatus
                this.middle.seckillStockNum = data.middle.seckillStockNum
                this.middle.isPurchase = data.middle.isPurchase
                this.middle.purchaseNum = data.middle.purchaseNum
                this.middle.isExpressage = data.middle.isExpressage
              }
            })
          }
        })
      },
      // 表单提交
      middleSubmit () {
        this.$refs['middle'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/seckill/middle/${!this.middle.seckillMiddleId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'seckillMiddleId': this.middle.seckillMiddleId || undefined,
                'shopId': this.middle.shopId,
                'seckillId': this.middle.seckillId,
                'orderId': this.middle.orderId,
                'seckillPrice': this.middle.seckillPrice,
                'seckillStatus': this.middle.seckillStatus,
                'seckillStockNum': this.middle.seckillStockNum,
                'isPurchase': this.middle.isPurchase,
                'purchaseNum': this.middle.purchaseNum,
                'isExpressage': this.middle.isExpressage
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
